<?php
namespace services;

class WmSms implements SmsInterface
{
    private $config = '';
    private $parameter = '';

    public function __construct ($config = array())
    {
        $this->config = array_merge(config('sms.wm'), $config);
    }
    public function setConfigs(array $configs = [])
    {
        $this->config = array_merge($this->config, $configs);
        return $this;
    }
    /**
     * 发送验证码
     * @param  string $phone       手机号码
     * @param  numeric  $code      验证码
     * @param  int $active_time    有效时间
     * @return bool                 true/false
     */
    public function sendCaptcha ($phone, $code, $active_time)
    {
        $this->parameter = array(
                'uid' => $this->config['account'],
                'pas' => $this->config['pass'],
                'mob' => $phone,
                'cid' => $this->config['cid'],
                'type' => $this->config['type'],
                'p1' => $code,
                'p2' => $active_time
        );
        return $this->send();
    }
    /**
     * 发送订单信息
     * @param  [type] $phone [description]
     * @param  [type] $code  [description]
     * @return [type]        [description]
     */
    public function sendOrder ($phone, $code)
    {
        $this->parameter = array(
                'uid' => $this->config['account'],
                'pas' => $this->config['pass'],
                'mob' => $phone,
                'cid' => $this->config['cid'],
                'type' => $this->config['type'],
                'p1' => $code
        );
        return $this->send();
    }
    public function sales($phone, $msg)
    {
        # code...
    }
    public function sendMessage($phone,$number)
    {
       $this->parameter = array(
                'uid' => $this->config['account'],
                'pas' => $this->config['pass'],
                'mob' => $phone,
                'cid' => $cid,
                'p1' => $number,

        );
       return $this->send();

    }

    protected function send()
    {
        if ($this->config['method'] == 'post') {
            $r = \services\Http::post($this->config['url'], $this->parameter);
        } else {
            $r = \services\Http::get($this->config['url'], $this->parameter);
        }
        if ($this->config['type'] == 'json') {
            $r = json_decode($r);
            if ($r->code == 0) {
                //Log::info('sendcode',['phone'=>$phone,'code'=>$code]);
                return true;
            }
        } else {
            if ($r == 0) {
                return true;
            }
        }
        return false;
    }


}